#----------------------------------------------------------------------------
# Global options
#----------------------------------------------------------------------------
# Add a debug dump to the option() function
function(OPTION name)
	_option(${name} ${ARGN})
	message("-- [Option] ${name} is set to ${${name}}")
endfunction(OPTION)

#----------------------------------------------------------------------------
# Compiler flags
#----------------------------------------------------------------------------
# Visual studio specific flags
if(MSVC)
	# Treat all warnings as errors
#  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
endif(MSVC)

# GCC specific flags
if(CMAKE_COMPILER_IS_GNUCXX)
	# Enable all warnings
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-value")
		# Treat all warnings as errors
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
    # Enable C++11 standard
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)

#----------------------------------------------------------------------------
# Environment paths
#----------------------------------------------------------------------------
# DEV_ROOT
file(TO_CMAKE_PATH "$ENV{DEV_ROOT}" DEV_ROOT)

# ENV_ROOT
file(TO_CMAKE_PATH "$ENV{ENV_ROOT}" ENV_ROOT)

# EXPORT_PATH
if(CMAKE_CL_64)
	set(EXPORT_PATH ${DEV_ROOT}/cmake_exports/x64/${CMAKE_BUILD_TYPE})
else(CMAKE_CL_64)
	set(EXPORT_PATH ${DEV_ROOT}/cmake_exports/x86/${CMAKE_BUILD_TYPE})
endif(CMAKE_CL_64)

#----------------------------------------------------------------------------
# External libraries
#----------------------------------------------------------------------------
# Qt5
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Subsequents modules are found by projects depending on their specific needs
# e.g: find_package(Qt5Widgets), find_package(Qt5Core) ... target_link_libraries(helloworld Qt5::Widgets)

#----------------------------------------------------------------------------
# Installation directories
#----------------------------------------------------------------------------
set(BINARY_INSTALL_DIR  bin)
set(LIBRARY_INSTALL_DIR lib)
set(INCLUDE_INSTALL_DIR include)
set(SHARED_INSTALL_DIR share)

#----------------------------------------------------------------------------
# Overriding functions & setting up global macros
#----------------------------------------------------------------------------
# add_library
function(add_library name)
  _add_library(${name} ${ARGN})

  # Auto-export target
  if(NOT ARGV2 OR NOT "${ARGV2}" STREQUAL "IMPORTED")
	export(TARGETS ${name} FILE ${EXPORT_PATH}/${name}.cmake)
	message("-- [Export] ${name} has been exported to ${EXPORT_PATH}/${name}.cmake")
  endif(NOT ARGV2 OR NOT "${ARGV2}" STREQUAL "IMPORTED")
  
  # Auto set RPATH to ../lib for MacOS and . for Windows & Linux.
  if(APPLE)
    set_target_properties(${name} PROPERTIES INSTALL_NAME_DIR "@executable_path/../lib")
  endif(APPLE)
  # CMake >= 3.1.0 does not allow to set the INSTALL_RPATH property on INTERFACE_LIBRARY target type
  get_target_property(TARGET_TYPE ${name} TYPE)
  if(NOT "${TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY")
    set_target_properties(${name} PROPERTIES INSTALL_RPATH "$ORIGIN")
    if(MSVC)
      set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}")
      set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}")
      set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_BINARY_DIR}")
      set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_BINARY_DIR}")
    endif(MSVC)
  endif(NOT "${TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY")
endfunction(add_library)

# add_executable
function(add_executable name)
  _add_executable(${name} ${ARGN})
  
  # Auto set RPATH to ../lib for MacOS and Linux and to . for Windows
  if(APPLE)
    set_target_properties(${name} PROPERTIES INSTALL_NAME_DIR "@executable_path/../lib")
  endif(APPLE)
  if(LINUX OR APPLE)
    set_target_properties(${name} PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
  else(LINUX OR APPLE)
    set_target_properties(${name} PROPERTIES INSTALL_RPATH "$ORIGIN")
  endif(LINUX OR APPLE)
  if(MSVC)
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}")
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}")
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_BINARY_DIR}")
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_BINARY_DIR}")
  endif(MSVC)
endfunction(add_executable)

# Import a module by name
macro(IMPORT_TARGET name)
	include(${EXPORT_PATH}/${name}.cmake)
	include_directories(${DEV_ROOT}/${name}/include)
	message("-- [Import] Importing module: ${name} (includes: ${DEV_ROOT}/${name}/include)")
endmacro(IMPORT_TARGET)
